﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class log : moveObject
    {
        protected int size;

        Texture2D mid; // hold textures for middle and end of log
        Texture2D end;

        public override Rectangle colRec
        {
            get
            {
                return new Rectangle((int)pos.X, (int)pos.Y, (tex.Width * 3), tex.Height);
                //use property to get rectangle from log
            }
        }

        public log(Direction dir, Level lvl, float time, float x, float y, float velx, int pace)
            : base(dir, time, x, y, velx, pace)
        {
            if (dir == Direction.Left)
                pos = new Vector2(x, y); // if the direction is left, set at right hand side, else set left
            else
                pos = new Vector2(x - 240, y);

            //loadContent(lvl);
        }

        public override void loadContent(Level lvl)
        {
            tex = lvl.Content.Load<Texture2D>("images/logs/l1");
            mid = lvl.Content.Load<Texture2D>("images/logs/l2");
            end = lvl.Content.Load<Texture2D>("images/logs/l3");
            // this method will contain code that will load the correct log or croc
            
        }

        public override void doSomething()
        {
            //base.doSomething();
            // deprecated
        }

        public virtual void setSpeed()
        {
            timeWait = tex.Width * 3 / Vel.X * 25 * pace; // set the time to wait while not on screen

        }


        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Vector2 posInc = pos;
            spriteBatch.Draw(tex, pos, Color.White);
            posInc.X += 80;
            spriteBatch.Draw(mid, posInc, Color.White);
            posInc.X += 80;
            //spriteBatch.Draw(mid, posInc, Color.White);
            //posInc.X += 80;
            spriteBatch.Draw(end, posInc, Color.White);
        }

        //public virtual checkLocation HandleCollisions(Level lvl, Player player)
        //{
        //    //return base.HandleCollisions(lvl, player);
        //}

        public virtual checkLocation HandleCollisions(Level lvl, Player player)
        {
            //Direction.
            //return base.HandleCollisions(lvl, player);
            Rectangle bounds = player.plyrRect;

            int playerX = (int)Math.Floor((float)player.Position.X / tile.width);
            int playerY = (int)Math.Ceiling((float)player.Position.Y / tile.height);
            // these cordinates are currently used to check what tile the player is on
            // i don't really understand what the math methods do

            // by using the playerx and playery variables, the enum variable check is assigned
            // what vis returned from the GetCollision method
            collisionCheck check = lvl.GetCollision(playerX, playerY);

            // if the player overlaps the log (calling it a truck for now)
            if (player.plyrRect.Intersects(colRec))
            {
                //if (player.plyrRect.Intersects(crocsHead))
                //   return checkLocation.crocbite;
                return checkLocation.onLog; // the method returns the enum checkLocation.onLog
            }
            //if (player.plyrRect.Intersects(colRec) && is
            else if (check != collisionCheck.passable) //else if the player is on a kill tile
            {
                //frogOn = false;
                return checkLocation.drowning; // then the drowning enum is returned (kills the player)
                //thing = checkLocation.drowning;
            }
            else // else the player is on ground
            {
                return checkLocation.onGround;
            }
        }
    }

    class largeLog : log
    {
        // different than log because this log is larger than the normal log
        Texture2D mid;
        Texture2D end;

        public override Rectangle colRec
        {
            get
            {
                //return base.colRec;
                return new Rectangle((int)pos.X, (int)pos.Y, (tex.Width * 4), tex.Height);
            }
        }

        public largeLog(Direction dir, Level lvl, float time, float x, float y, float velx, int pace, int size)
            : base(dir, lvl, time, x, y, velx, pace)
        {

            //loadContent(lvl);
            if (dir == Direction.Left)
                pos = new Vector2(x, y); // comment!!
            else
                pos = new Vector2(x - 320, y);
        }

        public override void loadContent(Level lvl)
        {
            //base.loadContent(lvl);

            tex = lvl.Content.Load<Texture2D>("images/logs/l1");
            mid = lvl.Content.Load<Texture2D>("images/logs/l2");
            end = lvl.Content.Load<Texture2D>("images/logs/l3");
        }

        public override void setSpeed()
        {
            //base.setSpeed();
            timeWait = tex.Width * 4/ Vel.X * 20 * pace;
        }

        //public override void Update(GameTime gameTime, Rectangle viewPort)
        //{
        //    //base.Update(gameTime, viewPort);
        //}

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Vector2 posInc = pos;
            //base.Draw(spriteBatch, gameTime);
            spriteBatch.Draw(tex, posInc, Color.White);
            posInc.X += 80;
            spriteBatch.Draw(mid, posInc, Color.White);
            posInc.X += 80;
            spriteBatch.Draw(mid, posInc, Color.White);
            posInc.X += 80;
            spriteBatch.Draw(end, posInc, Color.White);
        }


    }

    class smallLog : log
    {
        Texture2D end;
        public smallLog(Direction dir, Level lvl, float time, float x, float y, float velx, int pace, int size)
            : base(dir, lvl, time, x, y, velx, pace)
        {

            //loadContent(lvl);
        }

        public override void loadContent(Level lvl)
        {
            //base.loadContent(lvl);
            tex = lvl.Content.Load<Texture2D>("images/logs/l1");
            end = lvl.Content.Load<Texture2D>("images/logs/l3");
        }

        public override Rectangle colRec
        {
            get
            {
                //return base.colRec;
                return new Rectangle((int)pos.X, (int)pos.Y, (tex.Width * 2), tex.Height);
            }
        }

        //public override void Update(GameTime gameTime, Rectangle viewPort)
        //{
        //    //base.Update(gameTime, viewPort);

        //}

        public override void setSpeed()
        {
            //base.setSpeed();
            timeWait = tex.Width * 4 / Vel.X * 20 * pace;
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //base.Draw(spriteBatch, gameTime);
            //spriteBatch.Draw();

            Vector2 posInc = pos;
            //base.Draw(spriteBatch, gameTime);
            spriteBatch.Draw(tex, posInc, Color.White);
            posInc.X += 80;
            spriteBatch.Draw(end, posInc, Color.White);
        }

    }
}
